package com.wk.slaveNodes;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.BlockingQueue;

import com.jyt.bsd.TaskProcess;
import com.jyt.pt.engine.JobProgressMonitor;
import com.wk.job.Job;
import com.wk.repeaters.FixedIntervalRepeater;

public enum CalculateAJob {
	Calculate;

	private Job job = null;

	private CalculateAJob() {
		System.out.println("初始化计算环境...");
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("初始化完成...");
	}

	public boolean calculate(Job job) {
		this.job = job;
		String product_type = job.getJobName();
		String region_code = job.getJobProper();

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd E HH:mm:ss");
		String jobBeginTime = sdf.format(new Date());// 时间

		job.setJobBeginTime(jobBeginTime);
		System.out.println(job.getJobBeginTime() + "开始计算" + product_type + " " + region_code);

		final int UPDATE_JOB_PROGRESS_PERIOD = 1;// 更新Job进度的周期（单位：秒）
		JobProgressMonitor monitor = new JobProgressMonitor();
		BlockingQueue<Job> jobCompletionToken = monitor.monitor(job,
				new FixedIntervalRepeater(UPDATE_JOB_PROGRESS_PERIOD));

		TaskProcess taskP = new TaskProcess();
		taskP.setMonitor(monitor);
		taskP.process(product_type, region_code);

		try {
			jobCompletionToken.take();
		} catch (InterruptedException e1) {
		}

		System.out.println("完成！ " + job.getJobInfo());
		String jobEndTime = sdf.format(new Date());// 时间
		job.setJobEndTime(jobEndTime);
		System.out.println(job);
		return true;
	}

	public boolean isFinish() {
		if (job != null && job.getJobInfo() < 1) {
			return false;
		}
		return true;
	}

}
